package com.wc.算法基础课.A第一讲基础算法.前缀和与差分.重新排序;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/2 10:53
 * @description https://www.acwing.com/problem/content/4658/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010;
    // 原数组
    static long[] a = new long[N];
    // 差分数组(统计次数的)
    static int[] b = new int[N];
    // 前缀和数组
    static long[] s = new long[N];
    static int n, m, l, r;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
            s[i] = s[i - 1] + a[i];
        }
        m = sc.nextInt();
        long preSum = 0;
        for (int i = 1; i <= m; i++) {
            l = sc.nextInt();
            r = sc.nextInt();
            preSum += s[r] - s[l - 1];
            add();
        }
        long nowSum = 0;
        for (int i = 1; i <= n; i++) {
            b[i] += b[i - 1];
        }
        Arrays.sort(b, 1, n + 1);
        Arrays.sort(a, 1, n + 1);
        for (int i = 1; i <= n; i++) {
            nowSum += a[i] * b[i];
        }
        out.println(nowSum - preSum);
        out.flush();
    }

    static void add() {
        b[l] += 1;
        b[r + 1] -= 1;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
